package main

import (
       "os"
       "fmt"
)

type Args struct {
     key, value string
     ip_addr string
     partitionNumber int
}

type Reply struct {
     value string
}

type RPCRequest int

// Wrapper for handleRead based on RPC
func (t *RPCRequest) RPCRead(args *Args, reply *Reply) os.Error {
     reply.value = handleRead(args.key);
     return nil;
}

// Wrapper for handleWrite based on RPC
func (t *RPCRequest) RPCWrite(args *Args, reply *Reply) os.Error {
     fmt.Printf("Entered remote server\n");

     /*logs_seen_mutex.Lock();     
     start_point := logs_seen_index[args.partitionNumber][ip_serverid_map[args.ip_addr]];
     go sendLog(args.ip_addr, args.partitionNumber, start_point);
     logs_seen_index[args.partitionNumber][ip_serverid_map[args.ip_addr]] = getLogEnd(args.partitionNumber);
     logs_seen_mutex.Unlock();*/

     reply.value = handleWrite(args.key, args.value);
     return nil;
}

// Handles a read request
func handleRead(key string) string {
     if validateOperation(READ_OP, key, "") == true {
     	return readTable(key)
     }
     return "NIL"
}

// Handle a write request
func handleWrite(key string, value string) string {
     fmt.Println("Inside HandleWrite")
     if validateOperation(WRITE_OP, key, value) == true {
     	return writeTable(key, value)
     }
     return "NIL"
}
